package com.pepository.dao;

import com.aop.Detach;
import com.model.WordInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;



public interface WordInfoRepository extends JpaRepository<WordInfo, Long> {

    @Query(value = "select count(id) from word_info where word = :word", nativeQuery = true)
    int findCountByWord(@Param("word") String word);

    @Detach
    @Override
    <S extends WordInfo> S save(S entity);

    @Detach
    WordInfo findFirstByWord(String word);

    @Query( value =
            "select wi.id as wordId, wt.id as tableId, wi.word, wi.explained, wi.appear, wt.date as date from word_info as wi " +
            "join word_table as wt on wt.user_id = :userId and wt.word_info_id  = wi.id " +
            "order by wt.date desc, wt.id desc",
            countQuery =
            "select count(wt.id) from word_info as wi " +
            "join word_table as wt on wt.user_id = :userId and wt.word_info_id  = wi.id " +
            "order by wt.id",
            nativeQuery = true)
    Page<List<Map>> findUserPage(@Param("userId") Long userId, Pageable pageable);

}
